package com.bodu.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.enums.StatusFlagEnum;
import com.bodu.core.entity.result.Result;
import com.bodu.system.entity.po.StorageConfigPo;
import com.bodu.system.entity.request.StorageConfigRequest;
import com.bodu.system.entity.vo.StorageConfigVo;
import com.bodu.system.enums.StorageTypeEnums;
import com.bodu.system.mapper.StorageConfigMapper;
import com.bodu.system.service.StorageConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 存储配置 Service impl
 *
 * @author Administrator
 */
@Service
public class StorageConfigServiceImpl implements StorageConfigService {

    @Resource
    private StorageConfigMapper storageConfigMapper;

    @Override
    public StorageConfigVo getModel(Long id) {
        StorageConfigVo storageConfigVo = new StorageConfigVo();
        StorageConfigPo storageConfigPo = storageConfigMapper.getModel(id);
        BeanUtils.copyProperties(storageConfigPo, storageConfigVo);
        storageConfigVo.setStorageTypeName(StorageTypeEnums.getStatusValues(storageConfigVo.getStorageType()));
        return storageConfigVo;
    }

    @Transactional
    @Override
    public Long save(StorageConfigRequest request) {
        request.setStatusFlag(StatusFlagEnum.UN_USE.getValue());
        return storageConfigMapper.save(request);
    }

    @Transactional
    @Override
    public Long update(StorageConfigRequest request) {
        storageConfigMapper.update(request);
        return request.getId();
    }

    @Override
    public List<StorageConfigVo> listPage(ParamReq<StorageConfigRequest> params) {
        List<StorageConfigVo> res = storageConfigMapper.listPage(params);
        for (StorageConfigVo storageConfigPo : res) {
            storageConfigPo.setStorageTypeName(StorageTypeEnums.getStatusValues(storageConfigPo.getStorageType()));
            Integer statusFlag = storageConfigPo.getStatusFlag();
            storageConfigPo.setStatusFlagName(statusFlag.equals(StatusFlagEnum.USE.getValue()));
        }
        return res;
    }

    @Override
    public Integer countPage(ParamReq<StorageConfigRequest> params) {
        return storageConfigMapper.countPage(params);
    }


    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return storageConfigMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public Result<Integer> switchStatus(StorageConfigRequest request) {
        Integer statusFlag = request.getStatusFlag();
        Long id = request.getId();
        if (ObjectUtil.equal(StatusFlagEnum.USE.getValue(), statusFlag)) {
            storageConfigMapper.updateCloseAllStatus();
        }
        Integer res = storageConfigMapper.updateStatusFlagById(id, statusFlag);
        return Result.success(res);
    }
}
